const Koa = require("koa");
const app = new Koa();
const Router = require("koa-router");
const router = new Router();
const bodyparser = require("koa-bodyparser");
const JWT = require("jsonwebtoken");
const key = "2112A";

let orderList = require('./mock/orderlist');

let setImg = require('./mock/setImg');

//引入用户表数据
const userList = require("./mock/userInfor.json");

app.use(bodyparser());
app.use(router.routes()).use(router.allowedMethods());
app.listen(8088, () => {
  console.log("8088端口服务已启动！！！");
});

// router.get("/orderlist", (ctx) => {
//   const { val } = ctx.request.query;
//   if (val === "null") {
//     //列表
//     ctx.body = {
//       code: 200,
//       msg: "success",
//       orderList,
//     };
//   } else {
//     //搜索
//     ctx.body = {
//       code: 200,
//       msg: "success",
//       orderList: orderList.filter((item) => JSON.stringify(item).includes(val)),
//     };
//   }
// });

router.get("/orderlist", (ctx) => {
    //列表
    ctx.body = {
      code: 200,
      msg: "success",
      orderList,
    };
});

router.get("/setimg", (ctx) => {
    //列表
    ctx.body = {
      code: 200,
      msg: "success",
      setImg,
    };
});

//添加接口
router.post("/addlist", (ctx) => {
  const { id } = ctx.request.body;
  const item = orderList.find((item) => item.id === id);
  if (!item) {
    orderList.unshift(ctx.request.body);
    ctx.body = {
      code: 200,
      msg: "数据添加成功",
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "相同id的数据已存在",
    };
  }
});

//编辑接口
router.post("/editlist", (ctx) => {
  const { id } = ctx.request.body;
  const item = orderList.find((item) => item.id === id);
  if (item) {
    Object.assign(item, ctx.request.body);
    ctx.body = {
      code: 200,
      msg: "数据修改成功",
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "参数有误请重试",
    };
  }
});

// 删除
router.get('/del',ctx=>{
  const { id } = ctx.request.query;
  orderList = orderList.filter((item)=>!id.includes(item.id));
  ctx.body={
    code:200,
    msg:'删除成功'
  }
})

//post body get query 动态 params
//登陆接口
router.post("/login", (ctx) => {
  const { username, password } = ctx.request.body;

  const Item = userList.find((item) => item.username === username);

  if (Item) {
    if (Item.pasd === password) {
      //拷贝一份数据
      const NewData = { ...Item };
      //删除密码项
      delete NewData.pasd;

      //给前端响应数据
      ctx.body = {
        code: 200,
        msg: "登陆成功",
        userinfor: NewData,
        token: JWT.sign(NewData, key),
      };
    } else {
      ctx.body = {
        code: 400,
        msg: "密码错误请重新输入",
      };
    }
  } else {
    ctx.body = {
      code: 400,
      msg: "请先注册再登陆",
    };
  }
});

// 注册接口
router.post("/register", (ctx) => {
  const { username, password } = ctx.request.body;

  if (!username || !password) {
    ctx.body = {
      code: 400,
      msg: "手机号和密码是必需的",
    };
    return;
  }

  const existingUser = userList.find((item) => item.username === username);
  if (existingUser) {
    ctx.body = {
      code: 400,
      msg: "用户已存在",
    };
    return;
  }

  // 添加新用户
  const newUser = { username, pasd: password };
  userList.push(newUser);

  // 保存到 JSON 文件
  fs.writeFileSync(path.join(__dirname, './mock/userInfor.json'), JSON.stringify(userList, null, 2));

  ctx.body = {
    code: 200,
    msg: "注册成功",
  };
});
